// pages/stDetails/index.js
const app = getApp()
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    list: "",
    collect: 0,
    x: '375',
    y: "524",
    nowDate: "",
    show: false,//海报弹窗
    subscribe: false,//预约
    phone: "",
    username: "",
  },
  // 客服
  onServie() {
    wx.navigateTo({
      url: '/pages/service/index',
    })
  },
  onClickIcon() {
    if (this.data.collect == 0) {
      let data = {
        method: 'user.collect',
        mid: this.data.id,
        token: wx.getStorageSync('token'),
        mtype: 1
      }
      app.api.post('/api/index.html', data).then((res) => {
        if (res.status) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          this.setData({
            collect: 1
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    } else {
      let data = {
        method: 'user.collect',
        mid: this.data.id,
        token: wx.getStorageSync('token'),
        mtype: 1
      }
      app.api.post('/api/index.html', data).then((res) => {
        if (res.status) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          this.setData({
            collect: 0
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    }
  },

  teachers(e) {
    wx.navigateTo({
      url: '/pages/teacherDetails/index?id=' + e.currentTarget.dataset.id,
    })
  },
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
      wx.showToast({
        title: '已拒绝，获取失败',
        icon: 'none'
      })
    } else {
      wx.login({
        success: (res) => {
          if (res.code) {
            let data = {
              method: 'user.minimobile',
              token: wx.getStorageSync('token'),
              code: res.code,
              iv: e.detail.iv,
              edata: e.detail.encryptedData
            }
            app.api.post('/api/index.html', data).then((res) => {
              if (res.status) {
                this.setData({
                  phone: res.data.mobile
                })
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            })
          } else {
            wx.showToast({
              title: '获取失败',
              icon: 'none'
            })
          }
        }
      })
    }
  },
  //打开预约弹窗
  subscribe(e) {
    this.setData({ subscribe: true });
  },
  //关闭预约弹窗
  onCloseSubscribe() {
    this.setData({ subscribe: false });
  },
  onSubmit(e) {
    let data = {
      method: 'user.subscribe',
      token: wx.getStorageSync('token'),
      mobile: e.detail.value.phone,
      realname: e.detail.value.username,
      course_id: this.data.id
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        setTimeout(() => {
          this.getList()
          this.setData({ subscribe: false });
        }, 500)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  live() {
    if (this.data.list.live == undefined || this.data.list.live.room_id == null) {
      wx.showToast({
        title: '直播间暂未开放',
        icon: 'none'
      })
    } else if (this.data.list.hassubscribe == 0) {
      wx.showToast({
        title: '请先预约课程',
        icon: 'none'
      })
    } else {
      let roomId = this.data.list.live.room_id // 填写具体的房间号，可通过下面【获取直播房间列表】 API 获取
      let customParams = encodeURIComponent(JSON.stringify({ path: 'pages/live/index', pid: 1 })) // 开发者在直播间页面路径上携带自定义参数（如示例中的path和pid参数），后续可以在分享卡片链接和跳转至商详页时获取，详见【获取自定义参数】、【直播间到商详页面携带参数】章节（上限600个字符，超过部分会被截断）
      wx.navigateTo({
        url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var windowHeight = wx.getSystemInfoSync()
    var nowDate = util.formatTime(new Date())
    var formatTimeS = new Date(nowDate).getTime();
    this.setData({
      y: windowHeight.windowHeight - 200,
      x: windowHeight.windowWidth,
      id: options.id,
      nowDate: formatTimeS / 1000
    })
    this.getList()
  },
  getList() {
    let data = {
      method: 'course.getzhiinfo',
      id: this.data.id,
      token: wx.getStorageSync('token'),
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          list: res.data,
          collect: res.data.hascollect
        })
      }
    })
  },
  getUser() {
    let data = {
      method: 'user.info',
      token: wx.getStorageSync('token')
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          phone: res.data.mobile,
          username: res.data.realname,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
    this.getUser()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})